package com.ds.dsm.aggregation.config.data.tree;

import com.ds.common.JDSException;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.tree.TreeBaseDataView;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.foldingtree.NavFoldingTreeViewBean;
import com.ds.esd.custom.bean.nav.tree.NavTreeViewBean;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.TreeDataBaseBean;
import com.ds.esd.custom.form.pop.PopTreeDataBean;
import com.ds.esd.custom.form.pop.PopTreeViewBean;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/dsm/agg/entity/config/tree/")

public class TreeDataService {


    @MethodChinaName(cname = "清空列表配置")
    @RequestMapping(method = RequestMethod.POST, value = "clearData")
    @APIEventAnnotation(callback = {CustomCallBack.Reload}, bindMenu = CustomMenuItem.formReSet)
    public @ResponseBody
    ResultModel<Boolean> clearData(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(classConfig);
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        }

        return result;
    }

    @MethodChinaName(cname = "配置信息")
    @RequestMapping(method = RequestMethod.POST, value = "updateTreeDataBase")
    @APIEventAnnotation(callback = {CustomCallBack.Reload}, bindMenu = CustomMenuItem.formSave)
    public @ResponseBody
    ResultModel<Boolean> updateTreeDataBase(@RequestBody TreeBaseDataView dataView) {

        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(dataView.getSourceClassName(), dataView.getViewInstId());
            MethodConfig customMethodAPIBean = classConfig.getSourceConfig().getMethodByName(dataView.getMethodName());
            TreeDataBaseBean customTreeDataBean = null;

            if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                customTreeDataBean = (TreeDataBaseBean) customMethodAPIBean.getDataBean();
            } else if (customMethodAPIBean.getView() instanceof NavFoldingTreeViewBean) {
                customTreeDataBean = (TreeDataBaseBean) customMethodAPIBean.getDataBean();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                customTreeDataBean = (PopTreeDataBean) customMethodAPIBean.getDataBean();
            } else {
                customTreeDataBean = (TreeDataBaseBean) customMethodAPIBean.getDataBean();
            }
            customTreeDataBean.setFieldCaption(dataView.getFieldCaption());
            customTreeDataBean.setFieldId(dataView.getFieldId());
            customTreeDataBean.setRootId(dataView.getRootId());
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(classConfig);
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        }

        return result;
    }


}
